image.png

"Thirty days has November. April, June, and September. Of twenty eight is but one. And all the rest are thirty one. Of course leap year comes and slays. Every four years got it right, and twenty eight is twenty nine." ―Calendar Man

Calendar Man has always been one of the more fascinating Batman villains. A highly intelligent individual who plans all his crimes with reference to his calendar and the important dates that human civilisation has attached to these days and months. Clearly, Calendar Man sees the importance of time in his framework of crime, and this no different for someone studying data.

Time Series

What is a time series?

time series

noun

"A series of values of a quantity obtained at successive times, often with equal intervals between them."

What are the applications of studying time series data?

  • To obtain an understanding of the underlying structure that produced the data.
  • To fit a model to begin to forecast, monitor or even provide feedback control.

There are plenty of instances in which the observed data contains a temporal element. Applying time series analysis can provide applicative functions to areas such as:

  • Economic Forecasting
  • Sales Forecasting
  • Budgetary Analysis
  • Stock Market Analysis
  • Yield Projections
  • Process and Quality Control
  • Inventory Studies
  • Workload Projections
  • Utility Studies
  • Census Analysis

Steps in Time Series Analysis

Something here about decomposing the time series

Importing Data

In [2]:
# Basic packages
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import random as rd # generating random numbers
import datetime # manipulating date formats
# Viz
import matplotlib.pyplot as plt # basic plotting
import seaborn as sns # for prettier plots
import yfinance as yf


# TIME SERIES
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.tsa.statespace.sarimax import SARIMAX
from pandas.plotting import autocorrelation_plot
from statsmodels.tsa.stattools import adfuller, acf, pacf,arma_order_select_ic
import statsmodels.formula.api as smf
import statsmodels.tsa.api as smt
import statsmodels.api as sm
import scipy.stats as scs
from statsmodels.tsa.seasonal import seasonal_decompose


# settings
import warnings
warnings.filterwarnings("ignore")
plt.rcParams['figure.figsize'] = (20.0, 10.0)
plt.style.use("seaborn-deep")

Import Stock Data

In Construction: We will be using Apple Stock Data to illustrate some of the key methods and applications of various time series anlysis methods.

In [3]:
apple = yf.Ticker("AAPL")
apple = apple.history(period="1m",start="2000-1-1",end="2020-1-1")

# Importing with yfinance does not set frequency. Important when inputting to other models.
apple = apple.asfreq('B',method='ffill')

Smoothing the data

Moving Averages

Some information here regarding moving averages

Simple Moving Average

One method... Simple Moving Average

In [4]:
apple["Close"].plot(figsize=(16,10),title="Apple Closing Prices from 2000 to 2010",label='Apple')
apple["Close"].rolling(window=200).mean().plot(legend="200 month moving average",label="200 MA")
plt.legend()
Out[4]:
<matplotlib.legend.Legend at 0x12ccc3be0>

Exponential Moving Averages

In [5]:
None
In [6]:
apple["Close"].pct_change().plot(figsize=(16,10),title="Apple Returns")
Out[6]:
<AxesSubplot:title={'center':'Apple Returns'}, xlabel='Date'>

Decomposing Time Series

In [7]:
# decomposes the apple stock returns

result = (seasonal_decompose(pd.DataFrame(apple["2000":]['Close']),period=365))
fig = result.plot()

Useful Exploratory Data Analysis Visualisations

Closing Stock Prices Grouped by Month

In [8]:
# group by month (with names using the month_name method), on the Close column and find the mean of each month, sort and plot values

apple.groupby(apple.index.month_name(),)["Close"].mean().sort_values().plot.bar(rot=45,ylabel="Apple Closing Price",title="Mean Closing Price by Month From 1999-2020")
Out[8]:
<AxesSubplot:title={'center':'Mean Closing Price by Month From 1999-2020'}, xlabel='Date', ylabel='Apple Closing Price'>
In [ ]: